package cz.acies.uniql.sql;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.metadata.DBMetaData;
import cz.acies.uniql.metadata.FieldMetaData;
import cz.acies.uniql.metadata.PrimaryKeyMetaData;

public class AlterTable extends Query
{
	/*
	"EXEC sp_rename '[dbo].[users_main].[IDU_WRITER]', 'IDU_WRITER_O', 'COLUMN';",
	"ALTER TABLE dbo.users_main ADD [IDU_WRITER] varbinary(30) NULL;",
	"UPDATE dbo.users_main SET [IDU_WRITER] = CAST(IDU_WRITER_O AS varbinary(30));",
	"ALTER TABLE dbo.users_main DROP COLUMN IDU_WRITER_O; \n",
	*/
	
	// ----- Class Variables --------------------------------------------------
	
	private static final long serialVersionUID = -1291857437267456536L;
	
	public static final String ADD_COLUMN = "ADD_COLUMN";
	public static final String MODIFY_COLUMN = "MODIFY_COLUMN";
	public static final String CHANGE_COLUMN = "CHANGE_COLUMN";
	public static final String DROP_COLUMN = "DROP_COLUMN";
	
	public static final String ADD_PRIMARY_KEY = "ADD_PRIMARY_KEY";
	public static final String DROP_PRIMARY_KEY = "DROP_PRIMARY_KEY";

	public static final String ADD_INDEX = "ADD_INDEX";
	public static final String DROP_INDEX = "DROP_INDEX";

	public static final String ADD_FULLTEXT = "ADD FULLTEXT";
	public static final String DROP_FULLTEXT = "DROP FULLTEXT";
	
	private String schema = null;
	private String tableName = null;
	private String actionType = null;
	private FieldMetaData fmd = null;
	private PrimaryKeyMetaData pkmd = null;
	private Map<String, Object> idxmd = null;
	
	public final static Set<String> actionSet = new HashSet<String>(); {
		actionSet.add("ADD_COLUMN");
		actionSet.add("MODIFY_COLUMN");
		actionSet.add("CHANGE_COLUMN");
		actionSet.add("DROP_COLUMN");
		actionSet.add("ADD_PRIMARY_KEY");
		actionSet.add("DROP_PRIMARY_KEY");
		actionSet.add("ADD_INDEX");
		actionSet.add("ADD_FULLTEXT");
		actionSet.add("DROP_FULLTEXT");
		
	}
	// ----- Contructors ------------------------------------------------------
	
	public AlterTable() {
	}

	public AlterTable(Dialect dialect, DBMetaData dbmd) {
		super(dialect, dbmd);
	}

	public String toSQLString() throws Exception {
		return this.translate();
	}
	
	public String translate() throws Exception {
		return "";
	}

	// -----  -----------------------------------------------------------------
	
	public String getSchema() {
		if (this.schema == null) {
			if (this.getDialect() != null) {
				this.schema = this.getDialect().getSchema();	
			}
		}		
		return schema;
	}

	public AlterTable setSchema(String schema) {
		this.schema = schema;
		return this;
	}

	public String getTableName() {
		return tableName;
	}

	public AlterTable setTableName(String tableName) {
		this.tableName = tableName;
		return this;
	}

	public String getActionType() {
		return actionType;
	}

	public AlterTable setActionType(String actionType) {
		if (actionType != null && actionType.equals("CHANGE_COLUMN")) actionType = AlterTable.MODIFY_COLUMN;
		this.actionType = actionType;
		return this;
	}

	public AlterTable addField(FieldMetaData fmd) {
		this.fmd = fmd;
		return this;
	}

	public AlterTable addField(String name) {
		this.fmd = new FieldMetaData();
		fmd.setDialect(getDialect());
		fmd.setTable(this.tableName);
		fmd.setName(name);		
		return this;
	}
	
	public AlterTable addField(String name, int sqlType, int size, boolean isNull, String value) throws Exception {
		addField(name, sqlType, size, 0, isNull, value);
		return this;
	}
	
	public AlterTable addField(String name, int sqlType, int size, int decimal, boolean nullable, String value) throws Exception {
		this.fmd = new FieldMetaData();
		fmd.setDialect(getDialect());
		
		fmd.setTable(this.tableName);
		fmd.setName(name);
		fmd.setSqlType(sqlType);
		fmd.setSize(size);
		fmd.setDecimal(decimal);
		//fmd.setTypeName(rs.getString("TYPE_NAME"));
		//fmd.setIsNull(isNull);
		fmd.setNullable(nullable);
		fmd.setValue(value);
		
		return this;
	}

	// -----  -----------------------------------------------------------------
	
	public FieldMetaData fmd() {
		return fmd;
	}

	public FieldMetaData getFieldMetaData() {
		return this.fmd();
	}
	
	public void setFieldMetaData(FieldMetaData fmd) {
		this.fmd = fmd;
	}

	// -----  -----------------------------------------------------------------
	
	public PrimaryKeyMetaData pkmd() {
		return pkmd;
	}
	
	public PrimaryKeyMetaData getPKMetaData() {
		return this.pkmd();
	}

	public void setPKMetaData(PrimaryKeyMetaData pkmd) {
		this.pkmd = pkmd;
	}

	// -----  -----------------------------------------------------------------
	
	/**
	 * @return the idxmd
	 */
	public Map<String, Object> getIndex() {
		return idxmd;
	}

	/**
	 * @param idxmd the idxmd to set
	 */
	public AlterTable addIndex(Map<String, Object> idxmd) {
		this.idxmd = idxmd;
		return this;
	}

	// -----  -----------------------------------------------------------------
	
	/**
	 * @return the idxmd
	 */
	public Map<String, Object> getFulltext() {
		return idxmd;
	}

	/**
	 * @param idxmd the idxmd to set
	 */
	public AlterTable addFulltext(Map<String, Object> idxmd) {
		this.idxmd = idxmd;
		return this;
	}

	/*
	@SuppressWarnings("unchecked")
	public AlterTable addIndex() {
		Class c = this.getClass();
		MIndex idx = (MIndex)c.getAnnotation(MIndex.class);
		System.out.println(idx.name() + " " + " " + " " + idx.unique());
		idxmd = new HashMap<String, Object>(); 
		idxmd.put("INDEX_NAME", idx.name());
		idxmd.put("ASC_OR_DESC", idx.type());
		idxmd.put("NON_UNIQUE", idx.unique());
		Map<Integer, String> columns = new HashMap<Integer, String>();
		for (int i = 0; i < idx.columns().length; i++) {
			columns.put(i, idx.columns()[i]);
		}
		idxmd.put("columns", columns);
		System.out.println("AlterTable.setIdxmd().idxmd = " + idxmd);
		return this;
	}
	*/
}
